﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Controls;
using CharacterTrailClient.Query.Item;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Model;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit.API.Word;

namespace CharacterTrailClient.Windows
{
    public partial class FormComponentScan : FormBase
    {
        private readonly List<TProductHeaderModel> _productHeaderModels = new List<TProductHeaderModel>();
        private readonly IProductPersist _productPersist = null;
        public FormComponentScan()
        {
            _productPersist = InstanceFactory.GetProductPersist();
            InitializeComponent();
            this.componentScanEdit1.InputBarCodeSuccessEnd += ComponentScanEdit1InputBarCodeSuccessEnd;
            this.gridControlProductHeaderModels.DataSource = _productHeaderModels;
        }

        public override string HeaderText
        {
            get
            {
                return "  元件扫描管理  ";
            }
        }

        private void ComponentScanEdit1InputBarCodeSuccessEnd(CharacterTrailLibrary.Model.ProductHeaderContainBase productHeaderContainBase)
        {
            AppendProductHeaderModel(productHeaderContainBase);
        }

        public void AppendProductHeaderModel(TProductHeaderModel productDetailModel)
        {
            _productHeaderModels.Add(productDetailModel);
            this.gridControlProductHeaderModels.RefreshDataSource();
        }

        private void CmdQuery1_Click(object sender, EventArgs e)
        {
            try
            {
                ClearProductHeaderModels();

                string condition = string.Format(" ProcedureNo='{0}' ", ComponentScanEdit.Component);
                condition += this.dateItemInputQuery.GetCondition();
                AppendProductHeaderModels(_productPersist.GetProductHeaderModels(condition));

            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        public void ClearProductHeaderModels()
        {
            _productHeaderModels.Clear();
            this.gridControlProductHeaderModels.RefreshDataSource();
        }

        public void AppendProductHeaderModels(List<TProductHeaderModel> productHeaderModels)
        {
            foreach (var productDetailModel in productHeaderModels)
            {
                 _productHeaderModels.Add(productDetailModel);
            }
            this.gridControlProductHeaderModels.RefreshDataSource();
        }
        

        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            try
            {
                string condition = string.Format(" ProcedureNo='{0}' ", ComponentScanEdit.Component);

                foreach (var control in this.groupControlBase.Controls)
                {
                    if (control is IQueryItem)
                    {
                        var queryItem = (IQueryItem)control;
                        condition += queryItem.GetCondition();
                    }
                }

                this.gridControlQuery.DataSource =
                 _productPersist.GetVComponentScanBaseModels(condition);
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }
    }
}